package com.example.jetpackcomposelazycolumn

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.view.WindowCompat
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.compose.rememberNavController
import com.example.jetpackcomposelazycolumn.models.Post
import com.example.jetpackcomposelazycolumn.ui.screens.ImageDetail
import com.example.jetpackcomposelazycolumn.ui.screens.ImageList
import com.example.jetpackcomposelazycolumn.ui.theme.JetpackComposeLazyColumnTheme

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            JetpackComposeLazyColumnTheme {

                //  jetpack compose navigation
                val navController = rememberNavController()
                NavHost(navController = navController, startDestination = "homeScreen", builder = {
                    composable(route = "homeScreen") {
                        ImageList(navController)
                    }
                    // restfully 方式带参数
//                    composable(
//                        route = "detailScreen/{imageUrl}",
//                        arguments = listOf(navArgument("imageUrl") {
//                            type = NavType.StringType
//                        })
//                    ) { navBackStackEntry ->
//                        val arguments = requireNotNull(navBackStackEntry.arguments)
//                        val imageUrl = arguments.getString("imageUrl")
//                        println(imageUrl)
//                        ImageDetail(post = Post(1, imageUrl!!, 500, 500))
//                    }
                    // 表单方式带参数
//                    composable("detailScreen") {
//                        val imageUrl =
//                            navController.previousBackStackEntry?.arguments?.getString("imageUrl")
//                        imageUrl?.let {
//                            println(imageUrl)
//                            ImageDetail(post = Post(1, imageUrl, 500, 500))
//                        }
//                    }
                    // 序列化 传递对象, 前提是将对象序列化 @Parcelize
                    composable("detailScreen") {
                        val post =
                            navController.previousBackStackEntry
                                ?.arguments?.getParcelable<Post>("post")
                        post?.let {
                            Log.d("TAG", "Post: $post")
                            ImageDetail(post = post)
                        }
                    }
                })

            }
        }
    }
}